﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ConferenceZoom;
using ConferenceZoom.Controllers;
using ConferenceZoom.Tests.DTDBService;
using System.IO;
using IndicoInterface;

namespace ConferenceZoom.Tests.Controllers
{
    /// <summary>
    /// Summary description for HomeControllerTest
    /// </summary>
    [TestClass]
    public class HomeControllerTest
    {
        public TestContext TestContext { get; set; }

        #region TestSetup
        [TestInitialize]
        public void InitTest()
        {
            FileInfo db = new FileInfo("ConfData_" + TestContext.TestName + ".sdf");
            File.Copy(@"..\..\..\DeepTalkDBService\ConferenceData.sdf", db.FullName);
            using (DTDBClient rc = new DTDBClient())
            {
                rc.SetDBFile(db);
            }
        }

        string GetTalkXML(string id)
        {
            /// Submit a talk for rendering -- should not crash! :-)

            AgendaInfo info = new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?showSession=all&showDate=all&view=xml&fr=no&confId=" + id);
            StringWriter sr = new StringWriter();
            info.Seralize(sr);
            return sr.ToString();
        }

        string GetTalkXML()
        {
            return GetTalkXML("a042880");
        }
        #endregion

        [TestMethod]
        public void TestNoTalks()
        {
            HomeController controller = new HomeController();
            ViewResult result = controller.Index((int?)null) as ViewResult;

            Assert.IsNotNull(result.ViewData.Model, "The model data should not be null");
            Assert.AreEqual(typeof(ConferenceZoom.DTDBService.BasicTalkInfo[]), result.ViewData.Model.GetType(), "Should be an array of basic talk info!");
            ConferenceZoom.DTDBService.BasicTalkInfo[] talks = (ConferenceZoom.DTDBService.BasicTalkInfo[])result.ViewData.Model;
            Assert.AreEqual((int) 0, talks.Length, "Wrong number of talks in the returned data!");
        }

        [TestMethod]
        public void TestNoUnrenderedTalks()
        {
            HomeController controller = new HomeController();
            ViewResult result = controller.Queued() as ViewResult;

            Assert.IsNotNull(result.ViewData.Model, "The model data should not be null");
            Assert.AreEqual(typeof(ConferenceZoom.DTDBService.BasicTalkInfo[]), result.ViewData.Model.GetType(), "Should be an array of basic talk info!");
            ConferenceZoom.DTDBService.BasicTalkInfo[] talks = (ConferenceZoom.DTDBService.BasicTalkInfo[])result.ViewData.Model;
            Assert.AreEqual((int)0, talks.Length, "Wrong number of talks in the returned data!");
        }

        [TestMethod]
        public void TestOneUnrenderedTalks()
        {
            using (DTDBClient db = new DTDBClient())
            {
                db.SubmitNewTalk(GetTalkXML(), "Talk 1", DateTime.Now);
            }

            HomeController controller = new HomeController();
            ViewResult result = controller.Queued() as ViewResult;

            ConferenceZoom.DTDBService.BasicTalkInfo[] talks = (ConferenceZoom.DTDBService.BasicTalkInfo[])result.ViewData.Model;
            Assert.AreEqual((int)1, talks.Length, "Wrong number of talks in the returned data!");
        }

        [TestMethod]
        public void About()
        {
            // Setup
            HomeController controller = new HomeController();

            // Execute
            ViewResult result = controller.About() as ViewResult;

            // Verify
            ViewDataDictionary viewData = result.ViewData;
            Assert.AreEqual("About Page", viewData["Title"]);
        }
    }
}
